home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH07 / PGM7_2.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-02-06  |  4.7 KB  |  196 lines

  1. ; Pgm7_2.asm - Numeric I/O.
  2. ;
  3. ; Randall Hyde
  4. ; 2/6/96
  5. ;
  6. ; The standard library routines do not provide simple to use numeric input
  7. ; routines.  This code demonstrates how to read decimal and hexadecimal values 
  8. ; from the user using the Getsm, ATOI, ATOU, ATOH, IsDigit, and IsXDigit routines.
  9.  
  10.  
  11.         .xlist
  12.         include     stdlib.a
  13.         includelib    stdlib.lib
  14.         .list
  15.  
  16. dseg        segment    para public 'data'
  17.  
  18. inputLine    byte    128 dup (0)
  19.  
  20. SignedInteger    sword    ?
  21. UnsignedInt    word    ?
  22. HexValue    word    ?
  23.  
  24. dseg        ends
  25.  
  26. cseg        segment    para public 'code'
  27.         assume    cs:cseg, ds:dseg
  28.  
  29. Main        proc
  30.         mov    ax, dseg
  31.         mov    ds, ax
  32.         mov    es, ax
  33.         meminit
  34.  
  35.  
  36. ; Read a signed integer value from the user.
  37.  
  38. InputInteger:    print
  39.         byte    "Input a signed integer value: ",0
  40.  
  41.         lesi    inputLine        ;Point es:di at inputLine buffer
  42.         gets                ;Read a line of text from the user.
  43.  
  44.         mov    bx, -1
  45. SkipSpcs1:    inc    bx
  46.         cmp    inputLine[bx], ' '    ;Skip over any leading spaces.
  47.         je    SkipSpcs1
  48.  
  49.         cmp    inputLine[bx], '-'    ;See if it's got a minus sign
  50.         jne    NoSign
  51.         inc    bx            ;Skip if a negative number
  52.  
  53. NoSign:        dec    bx            ;Back up one place.
  54. TestDigs:    inc    bx            ;Move on to next char
  55.         mov    al, inputLine[bx]
  56.         IsDigit                ;See if it's a decimal digit.
  57.         je    TestDigs        ;Repeat process if it is.
  58.  
  59.         cmp    inputLine[bx], ' '    ;See if we end with a reasonable
  60.         je    GoodDec            ; character.
  61.         cmp    inputLine[bx], ','
  62.         je    GoodDec
  63.         cmp    inputLine[bx], 0    ;Input line ends with a zero byte.
  64.         je    GoodDec
  65.         printf
  66.         byte    "'%s' is an illegal signed integer.  Please reenter.",cr,lf,0
  67.         dword    inputLine
  68.         jmp    InputInteger
  69.  
  70. ; Okay, all the characters are cool, let's do the conversion here.  Note that ES:DI
  71. ; is still pointing at inputLine.
  72.  
  73. GoodDec:    ATOI                ;Do the conversion
  74.         mov    SignedInteger, ax    ;Save the value away.
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. ; Read an unsigned integer value from the user.
  83.  
  84. InputUnsigned:    print
  85.         byte    "Input an unsigned integer value: ",0
  86.  
  87.         lesi    inputLine        ;Point es:di at inputLine buffer
  88.         gets                ;Read a line of text from the user.
  89.  
  90. ; Note the sneakiness in the following code.  It starts with an index of minus two
  91. ; and then increments it by one.  When accessing data in this loop it compares
  92. ; against locatoin inputLine[bx+1] which effectively starts bx at zero.  In the
  93. ; "TestUnsigned" loop below, this code increments bx again so that bx then 
  94. ; contains the index into the string when the action is occuring.
  95.  
  96.         mov    bx, -2
  97. SkipSpcs2:    inc    bx
  98.         cmp    inputLine[bx+1], ' '    ;Skip over any leading spaces.
  99.         je    SkipSpcs2
  100.  
  101. TestUnsigned:    inc    bx            ;Move on to next char
  102.         mov    al, inputLine[bx]
  103.         IsDigit                ;See if it's a decimal digit.
  104.         je    TestUnsigned        ;Repeat process if it is.
  105.  
  106.         cmp    inputLine[bx], ' '    ;See if we end with a reasonable
  107.         je    GoodUnSigned        ; character.
  108.         cmp    inputLine[bx], ','
  109.         je    GoodUnsigned
  110.         cmp    inputLine[bx], 0    ;Input line ends with a zero byte.
  111.         je    GoodUnsigned
  112.         printf
  113.         byte    "'%s' is an illegal unsigned integer.  Please reenter.",cr,lf,0
  114.         dword    inputLine
  115.         jmp    InputUnsigned
  116.  
  117. ; Okay, all the characters are cool, let's do the conversion here.  Note that ES:DI
  118. ; is still pointing at inputLine.
  119.  
  120. GoodUnsigned:    ATOU                ;Do the conversion
  121.         mov    UnsignedInt, ax        ;Save the value away.
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128. ; Read a hexadecimal value from the user.
  129.  
  130. InputHex:    print
  131.         byte    "Input a hexadecimal value: ",0
  132.  
  133.         lesi    inputLine        ;Point es:di at inputLine buffer
  134.         gets                ;Read a line of text from the user.
  135.  
  136. ; The following code uses the same sneaky trick as the code above.
  137.  
  138.         mov    bx, -2
  139. SkipSpcs3:    inc    bx
  140.         cmp    inputLine[bx+1], ' '    ;Skip over any leading spaces.
  141.         je    SkipSpcs3
  142.  
  143. TestHex:    inc    bx            ;Move on to next char
  144.         mov    al, inputLine[bx]
  145.         IsXDigit            ;See if it's a hex digit.
  146.         je    TestHex            ;Repeat process if it is.
  147.  
  148.         cmp    inputLine[bx], ' '    ;See if we end with a reasonable
  149.         je    GoodHex            ; character.
  150.         cmp    inputLine[bx], ','
  151.         je    GoodHex
  152.         cmp    inputLine[bx], 0    ;Input line ends with a zero byte.
  153.         je    GoodHex
  154.         printf
  155.         byte    "'%s' is an illegal hexadecimal value.  Please reenter.",cr,lf,0
  156.         dword    inputLine
  157.         jmp    InputHex
  158.  
  159. ; Okay, all the characters are cool, let's do the conversion here.  Note that ES:DI
  160. ; is still pointing at inputLine.
  161.  
  162. GoodHex:    ATOH                ;Do the conversion
  163.         mov    HexValue, ax        ;Save the value away.
  164.  
  165.  
  166.  
  167.  
  168. ; Display the results:
  169.  
  170.         printf
  171.         byte    "Values input:",cr,lf
  172.         byte    "Signed:   %4d",cr,lf
  173.         byte    "Unsigned: %4d",cr,lf
  174.         byte    "Hex:      %4x",cr,lf,0
  175.         dword    SignedInteger, UnsignedInt, HexValue
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183. Quit:        ExitPgm
  184. Main        endp
  185.  
  186. cseg            ends
  187.  
  188. sseg        segment    para stack 'stack'
  189. stk        db    1024 dup ("stack   ")
  190. sseg        ends
  191.  
  192. zzzzzzseg    segment    para public 'zzzzzz'
  193. LastBytes    db    16 dup (?)
  194. zzzzzzseg    ends
  195.         end    Main
  196.